package org.medicalinstrument.impl;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.medicalinstrument.bean.ProductVo;
import org.medicalinstrument.bean.RentCart;
import org.medicalinstrument.dao.RentCartDao;
import org.medicalinstrument.database.DataSourceUtils;

import java.sql.SQLException;
import java.util.List;

public class RentCartDaoImpl implements RentCartDao {
    private QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
    RentCart cart = new RentCart();

    @Override
    public Boolean add(RentCart cart) throws SQLException {
        String sql = "insert into rentcart(custPhone," +
                "accountNO,productQR,rentDays,price,rentTime)" +
                "values (?,?,?,?,?,?)";

        //conn.setAutoCommit(false);
        int update = runner.update(sql, cart.getCustPhone(), cart.getAccountNo(), cart.getProductQR(), cart.getRentDays(), cart.getPrice(), cart.getRentTime());
        //conn.commit();
//conn.rollback();
        return update > 0;
    }

    @Override
    public RentCart getByAccountNo(String accountNo) throws SQLException {
        RentCart cart = new RentCart();
        String sql = "select distinct pro.*,r.rentDays,r.price,r.rentTime from rentCart as r join product as pro where pro.productQR=r.productQR AND r.accountNo = ?";
        List<ProductVo> pros = runner.query(sql, new BeanListHandler<ProductVo>(ProductVo.class), accountNo);
        cart.setProducts(pros);
        cart.setAccountNo(accountNo);
       // System.out.println("dao-----"+cart);
        return cart;
    }

    @Override
    public Boolean isRecordExists(String accountNo, String productQR) throws SQLException {
        String sql = "select * from rentcart where accountNo = ? and productQR = ?";
        RentCart record = runner.query(sql, new BeanHandler<RentCart>(RentCart.class), accountNo, productQR);
        return record != null;
    }
    //查询用户出车记录
    @Override
    public RentCart getByRecord(String accountNo, String productQR) throws SQLException {
        String sql = "select * from rentcart where accountNo = ? and productQR = ?";
        RentCart record = runner.query(sql, new BeanHandler<RentCart>(RentCart.class), accountNo, productQR);
        return record;
    }
    //修改租物车表记录
    @Override
    public Boolean update(RentCart cart) throws SQLException {
        String sql="update rentcart set rentTime=?,custPhone=?,rentDays=?,price=? where accountNo = ? and productQR = ? ";
        int update = runner.update(sql, cart.getRentTime(), cart.getCustPhone(), cart.getRentDays(), cart.getPrice(), cart.getAccountNo(), cart.getProductQR());
        return update>0;

    }
    //通过账号和二维码查询租物车记录和商品信息
    @Override
    public ProductVo getByRecordAll(String accountNo, String productQR) throws SQLException {
        String sql ="select distinct b.*,a.rentTime,a.rentDays,a.price from rentCart as a join product as b where a.productQR=b.productQR and accountNo = ? and a.productQR = ?";
        ProductVo pros = runner.query(sql, new BeanHandler<ProductVo>(ProductVo.class), accountNo,productQR);
        return pros;
    }

    //通过账号和商品二维码删除租物车表的记录
   @Override
    public RentCart deleteRecored(String accountNo, String productQR) throws SQLException {
       RentCart cart = getByRecord(accountNo, productQR);
       if (cart != null) {
           String sql2 = "delete from rentcart where accountNo = ? and productQR = ?";
           int b = runner.update(sql2, accountNo, productQR);
       }
       return cart;
   }
}